//
//  CustomerInfoViewController.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/4/29.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//   顾客角色－－我的

#import "CustomerInfoViewController.h"
#import "InfoViewController.h"
#import "BusinessViewController.h"
#import "SettingsViewController.h"
#import "PhootSelecteView.h"
#import "Tools.h"
#import "DealerDataManage.h"
#import "TxtReadVc.h"
#import "VPImageCropperViewController.h"
#import "CustomerInfoEditVc.h"
#import "MyOrderViewPageVc.h"

#import "CustomerDataManage.h"

#import "config.h"
#import "UIButton+WebCache.h"
#import "StringTool.h"
#import "CustomerOrderVC.h"
#import "UserModal.h"
#import "CalculatorViewController.h"
#import "MessBtn.h"
#import "MessageVC.h"
#import "MessageDM.h"



@interface CustomerInfoViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,VPImageCropperDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *mainView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIButton *headBtn;
@property (weak, nonatomic) IBOutlet UILabel *infoLab;
@property (strong, nonatomic) CustModel *model;

@property (nonatomic,strong)PhootSelecteView *menuView;
@property DealerDataManage *dm;
@property (nonatomic,strong)MessBtn *messBtn;
@property (weak, nonatomic) IBOutlet UIImageView *headImg;

@property (strong, nonatomic) CustomerDataManage  *dataManage;
@property (nonatomic,assign)BOOL needRefresh;

- (IBAction)clickBtn:(UIButton *)sender;


@end

@implementation CustomerInfoViewController
@synthesize menuView,dm;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    self.needRefresh = true;

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
     self.messBtn.badge = [NSString stringWithFormat:@"%ld",(long)[MessageDM getMessageNotReadCount]];
    
    
    if (self.needRefresh) {
        [self refreshData];
    }
    
    self.needRefresh = false;

     [self refreshData];
}

-(CustModel *)model
{
    if (!_model) {
        _model = [[CustModel alloc]init];
    }
    return _model;
}
-(void)initView
{
    self.mainView.showsVerticalScrollIndicator = NO;
   [self.titleView setMianTitle:@"我的"];
    [self.titleView.doneBtn removeFromSuperview];
    
    self.messBtn = [MessBtn buttonWithType:UIButtonTypeCustom];
    self.titleView.doneBtn.hidden = YES;
    [self.messBtn setFrame:CGRectMake(PMW-60,0,60, 44)];
    [self.messBtn setShowsTouchWhenHighlighted:YES];
    [self.messBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [self.messBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
    [self.messBtn setImage:[UIImage imageNamed:@"messbtn"] forState:UIControlStateNormal];
   
    [self.messBtn addTarget:self action:@selector(messBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.titleView addSubview:self.messBtn];
    self.mainView.frame = CGRectMake(0, 0, UIScreenWidth, SCROLLVIWEHEIGHT);
    
    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headViewHandleSingleTap:)];
    [self.headerView addGestureRecognizer:singleTap];
    
    dm = [[DealerDataManage alloc]init];
    
}

-(void)messBtnOnClick{
    
    MessageVC *vc = [MessageVC new];
     vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(CustomerDataManage *)dataManage
{
    if (!_dataManage) {
        _dataManage = [[CustomerDataManage alloc]init];
    }
    return _dataManage;
}

-(void)refreshData{
    [super refreshData];
//    [MBProgressHUD showMessage:@"请稍等"];
//    [MBProgressHUD showMessage:@"请稍后" toView:self.view];
    
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [bself.dataManage getCustomerInfo];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUDForView:bself.view animated:YES];
//            [MBProgressHUD hideHUD];
            
            if (mod.resltState == ResultFail) {
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError){
                [MBProgressHUD showError:@"网络错误"];
            }else
            {
                bself.model = mod.dataMod;
                [bself upUI];
            }
        });
    });
}


#pragma mark 获取到数据后更新ui
-(void)upUI
{
    
    [self.headBtn setBackgroundColor:NoImgColor];
    
    [self.headBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:self.model.avatar] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"headtop_pic"]];
    
   
    if (![StringTool isBlankString:self.model.account]) {
        self.infoLab.text = self.model.realname;
    }
}


#pragma mark 跳转到我的资料那一页
-(void)headViewHandleSingleTap:(UITapGestureRecognizer*)sender{
    
    self.needRefresh = true;
    CustomerInfoEditVc *vc = [[CustomerInfoEditVc alloc]initWithNibName:@"CustomerInfoEditVc" bundle:nil];
     vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];

}


-(void)btnOnclick:(id)sender{
    
    if (sender==menuView.tackPicBtn) {
        
        // 跳转到相机
        NSUInteger sourceType = UIImagePickerControllerSourceTypeCamera;
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = NO;
        imagePickerController.sourceType = sourceType;
        imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:imagePickerController animated:YES completion:^{}];
        
    }else if(sender==menuView.selectePicBtn){
        
        // 跳转到相册
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = NO;
        
        imagePickerController.sourceType = sourceType;
        imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }
}



#pragma mark 上传头像
-(void)upImg:(UIImage*)img{
    
    [menuView hide];
    
    [dm upHeadImg:img View:self.headBtn];
}




#pragma mark  UiPickView delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    image = [Tools fixOrientation:image];
    
    int h = [Tools getHightDifPMsW:100/2 sh:100/2 nw:PMW];
    CGRect buf = CGRectMake(0, PMH/2-h/2,PMW, h);
    
    // 裁剪
    VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:image cropFrame:buf limitScaleRatio:6];
    imgEditorVC.delegate = self;
    [self presentViewController:imgEditorVC animated:YES completion:^{}];
    
}



- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}





#pragma mark VPImageCropperDelegate
- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)image {
    
    [cropperViewController dismissViewControllerAnimated:YES completion:^{}];
    
    image = [Tools imageWithImageSimple:image scaledToSize:CGSizeMake(100, 100)];
    
    [self upImg:image];
}



- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {
    
    [cropperViewController dismissViewControllerAnimated:YES completion:^{}];
}



#pragma mark 点击“交易管理，客户管理，线下下单申请，系统设置，关于我们
- (IBAction)clickBtn:(UIButton *)sender {
    NSInteger tag = sender.tag;
    switch (tag) {
        case 0: //点击头像
            if (self.menuView==nil) {
                menuView = [PhootSelecteView creadInstance];
                [menuView.tackPicBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
                [menuView.selectePicBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
            }
            [menuView showInView:self.view];

            break;
        case  1://我的订单
        {
            CustomerOrderVC *vc = [CustomerOrderVC new];
             vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case  2://系统设置
        {
            SettingsViewController *vc = [[SettingsViewController alloc]initWithNibName:@"SettingsViewController" bundle:nil];
             vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }

            break;
        case  3://房贷计算器
        {
         CalculatorViewController *vc = [CalculatorViewController new];
           vc.hidesBottomBarWhenPushed = YES;  
            [self.navigationController pushViewController:vc animated:YES];

        }
            break;
        default:
            break;
    }
}



@end
